import { history } from "umi";
import { parse, stringify } from 'query-string';

/**
 * 清空路由堆栈跳转到界面
 * @param path 
 */
export function clearJump(path: string) {
  history.go(-history.length);
  history.replace(path);
}

/**
 * 跳转到指定路由并替换当前路由
 * @param path 
 */
export function replaceJump(path: string) {
  history.replace(path);
}

/**
 * 跳转到指定路由
 * @param path 
 */
export function jump(path: string) {
  history.push(path);
}

/**
 * 回退
 */
export function back() {
  history.goBack();
}

/**
 * 返回路由
 * @returns 
 */
export function getRoutePath() {
  return location.hash;
}

export const LOGIN_PAGES_PATHS = ['/login', '/forgetpwd', '/register'];

/**
 * 替换路由中的参数
 * @param key 
 * @param value 
 */
export function replaceParam(key: string, value: string) {
  const searchMap = parse(location.search);
  searchMap[key] = value;
  const paths = location.href.split("?");
  return paths[0] + '?' + stringify(searchMap);
}